package cn.android.support.v7.lib.sin.crown.popu;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import cn.android.support.v7.lib.sin.crown.R;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;

/**
 * Created by 彭治铭 on 2018/1/22.
 */

public class TopSnackbar extends PopupWindow {
    private static TopSnackbar topSnackbar;

    public static TopSnackbar getInstance(Activity activity) {
        if (topSnackbar == null) {
            topSnackbar = new TopSnackbar(activity);
        }
        return topSnackbar;
    }


    private TopSnackbar(Activity activity) {
        super(activity);
        this.activity = activity;
        init();
    }

    Activity activity;
    public TopView topView;

    private void init() {
        topView = new TopView(activity);
        topView.setBackgroundResource(R.drawable.crown_drawable_snackbar);
        setContentView(topView);
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setFocusable(false);//false点击外部区域【外部区域点击能力正常，点击没有被拦截,返回键也不会被拦截。】，popuwindow不会消失。true 外部区域的点击能力popuwindow被拦截。即按键和触摸事件像dialog一样被拦截。
        setOutsideTouchable(true);//测试发现，这个属性基本没什么用(对效果么有一点影响)，以防万一还是设置成true,防止触摸popuwindow外区，popuwindow不消失。
        setAnimationStyle(R.style.crown_popuwindow_top);//从顶部出来，popuwindow的动画在style里面。是属于window级别的。
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//设置背景,这样触摸返回键 和 popuwindow触摸外区，会让popuwindow会被自动关闭【没有背景，触摸外区就不会自动消失】,前提setFocusable(true)
        setClippingEnabled(false);//解决被状态栏遮挡。true会被状态栏遮挡。false不会被状态栏遮挡。
        // 刷新
        update();
    }

    @Override
    public void showAsDropDown(View anchor) {
        //解决7.0显示不正确的问题，7.0以下和7.1都没有问题。
        if (Build.VERSION.SDK_INT == 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    int message = 101;
    Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            removeMessages(message);//防止线程错乱。
            if (msg.what == message) {
                if (TopSnackbar.this.isShowing()) {
                    dismiss();//定时关闭。
                }
            }
        }
    };

    //直接显示在最顶部
    //调用案例: TopSnackbar.getInstance(this).showTop("再按一次退出");
    public TopSnackbar showTop(String text) {
        if (text == null || text.trim().equals("")) {
            text = "再按一次退出";
        }
        topView.setText(text).invalidate();
        showAtLocation(getParentView(), Gravity.NO_GRAVITY, 0, 0);
        handler.removeMessages(message);
        handler.sendEmptyMessageDelayed(message, 1500);//1.5秒自动关闭。
        return this;
    }

    //个文本添加点击事件
    public TopSnackbar setOnclickListener(View.OnClickListener onclickListener) {
        topView.setOnClickListener(onclickListener);
        return this;
    }

    //获取xml文件最外层控件。
    public ViewGroup getParentView() {
        View decorView = activity.getWindow().getDecorView();//布局里面的最顶层控件，本质上是FrameLayout(帧布局)，FrameLayout.LayoutParams
        ViewGroup contentView = (ViewGroup) decorView.findViewById(android.R.id.content);//我们的布局文件。就放在contentView里面。contentView本质上也是FrameLayout(帧布局)，FrameLayout.LayoutParams
        View parent = contentView.getChildAt(0);//这就是我们xml布局文件最外层的那个父容器控件。
        return (ViewGroup) parent;
    }

    class TopView extends View {

        public TopView(Context context) {
            super(context);
            init();
        }

        public TopView(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
            init();
        }

        public String text = "";
        int textSize = ProportionUtils.getInstance().adapterInt(36);
        int textColor = Color.parseColor("#FFFFFF");
        Paint paint;

        private void init() {
            paint = new Paint();
            paint.setAntiAlias(true);
            paint.setDither(true);
            paint.setTextSize(textSize);
            paint.setColor(textColor);
            paint.setTextAlign(Paint.Align.CENTER);
            paint.setStyle(Paint.Style.FILL);
        }

        int width = (int) ProportionUtils.getInstance().realWidthPixels;
        int statusHeight = ProportionUtils.getInstance().getStatusHeight(getContext());
        int height = statusHeight + ProportionUtils.getInstance().adapterInt(10);
        int realHeight = statusHeight + height;

        int x = width / 2;
        int y = statusHeight + textSize;

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            setMeasuredDimension(width, realHeight);
        }

        @Override
        public void draw(Canvas canvas) {
            super.draw(canvas);
            canvas.drawText(text, x, y, paint);
        }

        public TopView setText(String text) {
            this.text = text;
            return this;
        }

        public TopView setTextSize(int textSize) {
            this.textSize = textSize;
            return this;
        }

        public TopView setTextColor(int textColor) {
            this.textColor = textColor;
            return this;
        }

        public TopView setHeight(int height) {
            this.height = height;
            return this;
        }

    }

}
